package com.zheng.upms.server.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.util.DateUtil;
import com.zheng.upms.common.constant.UpmsResult;
import com.zheng.upms.common.constant.UpmsResultConstant;
import com.zheng.upms.dao.model.UpmsScheduleJob;
import com.zheng.upms.dao.model.UpmsScheduleJobExample;
import com.zheng.upms.rpc.api.UpmsScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: UpmsJobController <br>
 * @Description: TODO <br>
 * @date 2019/7/14 18:08<br>
 **/
@Controller
@RequestMapping("/manage/job")
@Api(value = "任务调度控制器", description = "任务调度控制器")
public class UpmsJobController extends BaseController {


    @Autowired
    private UpmsScheduleJobService upmsScheduleJobService;

    @ApiOperation(value = "任务调度首页")
    @RequiresPermissions("upms:job:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/job/index.jsp";
    }

    @ApiOperation(value = "任务调度列表")
    @RequiresPermissions("upms:job:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, value = "jobName") String jobName,
            @RequestParam(required = false, value = "jobGroup") String jobGroup,
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UpmsScheduleJobExample example = new UpmsScheduleJobExample();
        UpmsScheduleJobExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }else{
            example.setOrderByClause(" create_time desc");
        }

        if(StringUtils.isNotBlank(jobName)){
            criteria.andJobNameLike("%"+jobName+"%");
        }
        if(StringUtils.isNotBlank(jobGroup)){
            criteria.andJobGroupLike("%"+jobGroup+"%");
        }
        List<UpmsScheduleJob> rows = upmsScheduleJobService.selectByExampleForOffsetPage(example, offset, limit);
        long total = upmsScheduleJobService.countByExample(example);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "创建任务调度页")
    @RequiresPermissions("upms:job:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        return "/manage/job/create.jsp";
    }

    @ApiOperation(value = "创建任务调度")
    @RequiresPermissions("upms:job:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UpmsScheduleJob upmsScheduleJob) {
        UpmsScheduleJobExample example = new UpmsScheduleJobExample();
        UpmsScheduleJobExample.Criteria criteria = example.createCriteria();
        criteria.andJobNameEqualTo(upmsScheduleJob.getJobName());
        criteria.andJobGroupEqualTo(upmsScheduleJob.getJobGroup());
        Integer count = upmsScheduleJobService.countByExample(example);
        if(count > 0){
            return new UpmsResult(UpmsResultConstant.FAILED,"已存在该任务。");
        }
        String loginUserName = getLoginUserName();
        upmsScheduleJob.setCreateTime(DateUtil.getDate());
        upmsScheduleJob.setCreateBy(loginUserName);
        upmsScheduleJob.setStatus(1);
        upmsScheduleJob.setRemoteRequestMethod("POST");

        try {
            count = upmsScheduleJobService.saveScheduleJob(upmsScheduleJob);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, count);
        }

        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改任务调度页")
    @RequiresPermissions("upms:job:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UpmsScheduleJob upmsScheduleJob = upmsScheduleJobService.selectByPrimaryKey(id);
        modelMap.put("upmsScheduleJob", upmsScheduleJob);
        return "/manage/job/update.jsp";
    }

    @ApiOperation(value = "修改任务调度")
    @RequiresPermissions("upms:job:update")
    @ResponseBody
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    public Object update(@PathVariable("id") int id,UpmsScheduleJob upmsScheduleJob) {
        upmsScheduleJob.setId(id);
        int count = 0;
        try {
            String loginUserName = getLoginUserName();
            upmsScheduleJob.setModifyTime(DateUtil.getDate());
            upmsScheduleJob.setModifyBy(loginUserName);
            count = upmsScheduleJobService.updateScheduleJob(upmsScheduleJob);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, count);
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除任务调度")
    @RequiresPermissions("upms:job:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = 0;
        try {
            count = upmsScheduleJobService.deleteScheduleJob(ids);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, count);
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "恢复任务调度")
    @RequiresPermissions("upms:job:resume")
    @ResponseBody
    @RequestMapping(value = "/resume/{id}", method = {RequestMethod.GET})
    public Object resume(@PathVariable("id") long id) {
        try {
            upmsScheduleJobService.resumeJob(id);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, e.getMessage());
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, "");
    }

    @ApiOperation(value = "恢复任务调度")
    @RequiresPermissions("upms:job:pause")
    @ResponseBody
    @RequestMapping(value = "/pause/{id}", method = {RequestMethod.GET})
    public Object pause(@PathVariable("id") long id) {
        try {
            upmsScheduleJobService.pauseJob(id);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, e.getMessage());
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, "");
    }

    @ApiOperation(value = "恢复任务调度")
    @RequiresPermissions("upms:job:runOnce")
    @ResponseBody
    @RequestMapping(value = "/runOnce/{id}", method = {RequestMethod.GET})
    public Object runOnce(@PathVariable("id") long id) {
        try {
            upmsScheduleJobService.runOnce(id);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, e.getMessage());
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, "");
    }
}
